using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SouthernLand.Common.BOL.Contact;

namespace SouthernLand.Common.DAL.Contact
{
    public class ContactSettingDAL : BaseDAL
    {
        #region ObjIDataReader

        /// <summary>
        /// Returns a new ContactSetting instance filled with the DataReader's current record data 
        /// </summary>
        private ContactSetting GetContactSettingFromReader(IDataReader dr)
        {
            var obj = new ContactSetting();
            obj.ContactSettingID = (dr["ContactSettingID"] is DBNull) ? Int32.MinValue : (Int32) dr["ContactSettingID"];
            obj.AdminEmail = (dr["AdminEmail"] is DBNull) ? String.Empty : (String) dr["AdminEmail"];
            obj.CompanyName = (dr["CompanyName"] is DBNull) ? String.Empty : (String) dr["CompanyName"];
            obj.Email = (dr["Email"] is DBNull) ? String.Empty : (String) dr["Email"];
            obj.Fax = (dr["Fax"] is DBNull) ? String.Empty : (String) dr["Fax"];
            obj.Phone = (dr["Phone"] is DBNull) ? String.Empty : (String) dr["Phone"];
            obj.Hotline = (dr["Hotline"] is DBNull) ? String.Empty : (String) dr["Hotline"];
            obj.Address = (dr["Address"] is DBNull) ? String.Empty : (String) dr["Address"];
            return obj;
        }

        #endregion

        /// <summary>
        /// Returns a collection of ContactSetting with the data read from the input DataReader
        /// </summary>
        private List<ContactSetting> GetContactSettingCollectionFromReader(IDataReader dr)
        {
            var obj = new List<ContactSetting>();
            while (dr.Read())
                obj.Add(GetContactSettingFromReader(dr));
            return obj;
        }

        public int Insert(ContactSetting obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("Job_ContactSetting_Insert", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@ContactSettingID", SqlDbType.Int).Direction = ParameterDirection.Output;
                myCommand.Parameters.Add("@AdminEmail", SqlDbType.VarChar).Value = obj.AdminEmail;
                myCommand.Parameters.Add("@CompanyName", SqlDbType.NVarChar).Value = obj.CompanyName;
                myCommand.Parameters.Add("@Email", SqlDbType.VarChar).Value = obj.Email;
                myCommand.Parameters.Add("@Fax", SqlDbType.VarChar).Value = obj.Fax;
                myCommand.Parameters.Add("@Phone", SqlDbType.VarChar).Value = obj.Phone;
                myCommand.Parameters.Add("@Hotline", SqlDbType.VarChar).Value = obj.Hotline;
                myCommand.Parameters.Add("@Address", SqlDbType.NVarChar).Value = obj.Address;

                int ret = myCommand.ExecuteNonQuery();
                return (int) myCommand.Parameters["@ContactSettingID"].Value;
            }
        }

        public bool Update(ContactSetting obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("Job_ContactSetting_Update", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@ContactSettingID", SqlDbType.Int).Value = obj.ContactSettingID;
                myCommand.Parameters.Add("@AdminEmail", SqlDbType.VarChar).Value = obj.AdminEmail;
                myCommand.Parameters.Add("@CompanyName", SqlDbType.NVarChar).Value = obj.CompanyName;
                myCommand.Parameters.Add("@Email", SqlDbType.VarChar).Value = obj.Email;
                myCommand.Parameters.Add("@Fax", SqlDbType.VarChar).Value = obj.Fax;
                myCommand.Parameters.Add("@Phone", SqlDbType.VarChar).Value = obj.Phone;
                myCommand.Parameters.Add("@Hotline", SqlDbType.VarChar).Value = obj.Hotline;
                myCommand.Parameters.Add("@Address", SqlDbType.NVarChar).Value = obj.Address;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        public bool Delete(Int32 ContactSettingID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("Job_ContactSetting_Delete", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@ContactSettingID", SqlDbType.Int).Value = ContactSettingID;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        /// <summary>
        /// Retrieves the Job_ContactSetting with the specified ID
        /// </summary>
        public ContactSetting GetContactSettingByID(Int32 ContactSettingID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("Job_ContactSetting_GetContactSettingByID", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@ContactSettingID", SqlDbType.Int).Value = ContactSettingID;

                SqlDataReader dr = myCommand.ExecuteReader();
                return (dr.Read() ? GetContactSettingFromReader(dr) : null);
            }
        }

        //Get All ContactSetting
        public List<ContactSetting> GetAllContactSetting(int PageIndex, int PageSize)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("Job_ContactSetting_GetAllContactSetting", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = PageIndex;
                myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSize;
                SqlDataReader dr = myCommand.ExecuteReader();
                return GetContactSettingCollectionFromReader(dr);
            }
        }

        // Get Count All records of Table  ContactSetting
        public int GetCountContactSetting()
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("Job_ContactSetting_GetCountContactSetting", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                return (int) myCommand.ExecuteScalar();
            }
        }
    }
}